import sys
import os
import datetime
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from models.company import Company


class CompanyService():

	class Index(webapp.RequestHandler):
		def get(self):
			path = os.path.join(os.path.dirname(__file__), '../views/companies/index.html')
			html = template.render(path, {'companies' : Company.all()})
			self.response.out.write(html)

	class Create(webapp.RequestHandler):
		def get(self):
			path = os.path.join(os.path.dirname(__file__), '../views/companies/create.html')
			html = template.render(path, {})
			self.response.out.write(html)
		def post(self):
			company = Company.get_by_name(str(self.request.get('name')))
			if company:
				self.response.out.write('the company name ' + company.name + ' is already in use')
				return
			if not company:
				try:
					company = Company()
					company.name        = str(self.request.get('name'))
					company.slug        = Company.create_slug(company.name)
					company.email       = str(self.request.get('email'))
					company.phone       = str(self.request.get('phone'))
					company.address     = str(self.request.get('address'))
					company.description = str(self.request.get('description'))
					company.website     = str(self.request.get('website'))
					company.put()
					self.response.out.write('ok')
					return
				except:
					exc_type, exc_value, exc_traceback = sys.exc_info()
					self.response.out.write(exc_value)
					return
				
	class Update(webapp.RequestHandler):
		def get(self, slug):
			company = Company.get_by_slug(str(slug))
			if company:
				path    = os.path.join(os.path.dirname(__file__), '../views/companies/update.html')
				html    = template.render(path, {'company' : company})
				self.response.out.write(html)
				return
			self.response.out.write('company does not exist')
	
		def post(self, slug):
			company = Company.get_by_slug(str(slug))
			if not company:
				self.response.out.write('the company name ' + slug + ' does not exist.')
				return
			if company:
				try:
					company.email       = str(self.request.get('email'))
					company.phone       = str(self.request.get('phone'))
					company.address     = str(self.request.get('address'))
					company.description = str(self.request.get('description'))
					company.website     = str(self.request.get('website'))
					company.put()
					self.response.out.write('ok')
					return
				except:
					exc_type, exc_value, exc_traceback = sys.exc_info()
					self.response.out.write(exc_value)
					return
		
	class Delete(webapp.RequestHandler):
		def get(self, slug):
			company = Company.get_by_slug(str(slug))
			if company:
				path = os.path.join(os.path.dirname(__file__), '../views/companies/delete.html')
				html = template.render(path, {'company' : company})
				self.response.out.write(html)
			else:
				self.response.out.write('this company does not exist')
		def post(self, slug):
			company = Company.get_by_slug(str(slug))
			if company:
				company.delete()
				self.response.out.write('ok')
				return
			self.response.out.write('company not found or no longer exists')